package cn.dgut.jy.soro.controller;

import cn.dgut.jy.soro.entity.User;
import cn.dgut.jy.soro.service.AppointmentService;
import cn.dgut.jy.soro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
public class AppointmentController {
    private AppointmentService appointmentService;
    private UserService userService;

    @Autowired
    public AppointmentController(AppointmentService appointmentService, UserService userService) {
        this.appointmentService = appointmentService;
        this.userService = userService;
    }

    @RequestMapping("/appointment")
    public String appointment (Model model, HttpSession session) {
        if(session.getAttribute("username") == null) {
            model.addAttribute("message", "请先登录");
            return "/login";
        }
        return "/appointment";
    }

    @RequestMapping(value = "/appointing", method = RequestMethod.POST)
    public void appointing(HttpServletRequest request, HttpServletResponse response, HttpSession session
            , @RequestParam String petType, @RequestParam String contentType, @RequestParam String address,
                    @RequestParam String store,@RequestParam String name, @RequestParam String date,
                    @RequestParam String demand) throws IOException, ParseException {
            if(session.getAttribute("username") == null || session.getAttribute("userId") == null) {
                response.sendRedirect(request.getContextPath() + "/login");
            }
            Integer userId = (Integer) session.getAttribute("userId");
            System.out.println(date);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            appointmentService.addAppointment(userId, petType, contentType, address, store,name, sdf.parse(date), demand);

        response.sendRedirect(request.getContextPath() + "/appointment");
    }

    @RequestMapping("/myAppointments")
    public String myAppointments(HttpSession session, Model model) {
        Integer userId = (Integer) session.getAttribute("userId");
        if(userId == null) {
            return "/login";
        }
        model.addAttribute("appointments", appointmentService.getAppointments(userId));
        return "/myAppointments";
    }

    @RequestMapping("/checkAppointments")
    public String checkAppointments(HttpSession session, Model model) {
        Integer userId = (Integer) session.getAttribute("userId");
        if(userId == null) {
            model.addAttribute("message", "请先登录");
            return "/login";
        }

        if(userService.getPersonalInfo(userId).getAuthority() != 2) {
            return "/personal";
        }

        model.addAttribute("appointments", appointmentService.getAllAppointments());
        return "checkAppointments";
    }


}
